package com.jwm.power.controller;

import cn.zhxu.bs.MapSearcher;
import cn.zhxu.bs.SearchResult;
import cn.zhxu.bs.util.MapUtils;
import com.jwm.power.mapper.QueryMapper;
import com.jwm.power.model.entity.Depts;
import com.jwm.power.model.entity.Menus;
import com.jwm.power.model.reponse.ResponseData;
import com.jwm.power.service.MenusService;
import com.jwm.power.util.Ut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//  菜单管理
@Controller
@RequestMapping("/menus")
public class MenusController {
    @Autowired
    private MenusService menusService;
    @Autowired
    private MapSearcher mapSearcher;              // 注入 BeanSearcher 的检索器
    @Autowired
    private QueryMapper queryMapper;

    //  获取菜单数据
    @RequestMapping(value = "/queryMenus", method = RequestMethod.GET)
    public void QueryMenus(HttpServletRequest request, HttpServletResponse response) {
        try {
            // 一行代码，实现一个用户检索接口（MapUtils.flat 只是收集前端的请求参数）
            String menuName = request.getParameter("sName");
            List<Map<String, Object>> searchResult  = new ArrayList<>();
            if (menuName.length() > 0) {
                Map<String, Object> objectMap = new HashMap<>();
                objectMap.put("strCell", "distinct menuid, menuname, menucode, jumpurl, is_effective, parentcode, orderbyid, iconstyle, menutype");
                objectMap.put("strChildCell", "c.menuid, c.menuname, c.menucode, c.jumpurl, c.is_effective, c.parentcode, c.orderbyid, c.iconstyle, c.menutype");
                objectMap.put("strWhere", " menutype=0 and menuname like '%" + menuName + "%'");
                objectMap.put("tName", " Menus");
                objectMap.put("strChildWhere", " t.menucode = c.parentcode");
                searchResult = queryMapper.queryWithDatas(objectMap);
            } else {
                searchResult = mapSearcher.searchAll(Menus.class, MapUtils.flat(request.getParameterMap()));
            }
            Map<String, Object> dataMap = new HashMap<>();
            List<Map<String, Object>> treeMap = new ArrayList<>();
            //  重新组装树形数据
            for (Map<String, Object> objectMap : searchResult) {
                Integer isParent = 0;
                for (Map<String, Object> checkMap : searchResult) {
                    if (checkMap.get("menucode").equals(objectMap.get("parentcode"))) {
                        isParent = 1;
                    }
                }
                if(isParent == 0){
                    Map<String, Object> itemMap = Ut.loadTreeData(objectMap, searchResult,"menucode");
                    treeMap.add(itemMap);
                }
            }
            dataMap.put("data", treeMap);
            Ut.toJson(response, dataMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //  设置菜单
    @RequestMapping(value = "saveMenus", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData SaveMenus(HttpServletRequest request, HttpServletResponse response) throws ParseException {
        return menusService.SaveMenus(request);
    }

}
